/*
 * *****************************************************************************
 *  Copyright ( c ) 2019 huibur Inc. All Rights Reserved.
 *
 *  This software is the confidential and proprietary information of Huibur Inc
 *  (*Confidential Information*). You shall not disclose such Confidential Information
 *  and shall use it only in accordance with the terms of the license agreement
 *  you entered into with Huibur or a Huibur authorized
 *  reseller (the "License Agreement*).
 *  ******************************************************************************
 */

package com.huibur.devicecheck.controller;

import com.huibur.devicecheck.enums.ResultCodeEnum;
import com.huibur.devicecheck.exception.*;
import com.huibur.devicecheck.model.param.TaskParam;
import com.huibur.devicecheck.model.query.TaskQuery;
import com.huibur.devicecheck.model.vo.TaskVO;
import com.huibur.devicecheck.request.ObjRes;
import com.huibur.devicecheck.request.PageRes;
import com.huibur.devicecheck.service.ITaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/*******************************************************
* Title: TaskController.java
* Description: 
* @author baiqingshuo
* @date 2021/9/23
* @version 1.0
*******************************************************/
@RestController
@RequestMapping("/tasks")
@Api(value = "任务管理",tags = {"任务管理"})
@Slf4j
public class TaskController {

    @Autowired
    ITaskService iTaskService;

    /**
     * 新增任务
     *
     * @param taskParam
     * @return
     */
    @PostMapping("")
    @ApiOperation(value = "新增任务", response = ObjRes.class)
    public ObjRes<Void> insert(@RequestBody TaskParam taskParam) {
        try {
            return iTaskService.insert(taskParam);
        } catch (TaskTitleRepeatException e) {
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.TASK_TITLE_REPEAT.getCode(), ResultCodeEnum.TASK_TITLE_REPEAT.getLabel());
        } catch (PlanEndTimeUnableLessThanPlanStartTimeException e) {
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.PLAN_END_TIME_UNABLE_LESS_THAN_PLAN_START_TIME.getCode(), ResultCodeEnum.PLAN_END_TIME_UNABLE_LESS_THAN_PLAN_START_TIME.getLabel());
        }
    }

    /**
     * 删除任务
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除任务", response = ObjRes.class)
    public ObjRes<Void> deleteById(@PathVariable Long id) {
        try {
            return iTaskService.deleteById(id);
        } catch (TaskIsNotProgressException e) {
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.TASK_IS_NOT_PROGRESS.getCode(), ResultCodeEnum.TASK_IS_NOT_PROGRESS.getLabel());
        }

    }

    /**
     * 编辑任务
     *
     * @param id
     * @param taskParam
     * @return
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "编辑任务", response = ObjRes.class)
    public ObjRes<Void> updateTask(@PathVariable Long id, @RequestBody TaskParam taskParam) {
        try {
            return iTaskService.updateTask(id, taskParam);
        } catch (TaskTitleRepeatException e) {
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.TASK_TITLE_REPEAT.getCode(), ResultCodeEnum.TASK_TITLE_REPEAT.getLabel());
        } catch (ActEndTimeUnableLessThanActStartTimeException e) {
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.ACT_END_TIME_UNABLE_LESS_THAN_ACT_START_TIME.getCode(), ResultCodeEnum.ACT_END_TIME_UNABLE_LESS_THAN_ACT_START_TIME.getLabel());
        }

    }

    /**
     * 任务列表(查询并分页)
     *
     * @param taskQuery
     * @return
     */
    @GetMapping("")
    @ApiOperation(value = "任务列表(查询并分页)", response = PageRes.class)
    public PageRes<TaskVO> selectTaskList(TaskQuery taskQuery) {
        return iTaskService.selectTaskList(taskQuery);
    }

    /**
     * 获取任务详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取任务详情", response = ObjRes.class)
    public ObjRes<TaskVO> selectTaskById(@PathVariable Long id) {
        return new ObjRes<TaskVO>().data(iTaskService.selectTaskById(id));
    }

    /**
     * 实际开始时间
     *
     * @return
     */
    @PutMapping("/startTime/{id}")
    @ApiOperation(value = "实际开始时间", response = ObjRes.class)
    public ObjRes<Void> startTime(@PathVariable Long id) {
        try {
            return iTaskService.startTime(id);
        } catch (TaskIsStartException e) {
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.TASK_IS_START.getCode(), ResultCodeEnum.TASK_IS_START.getLabel());
        }
    }

    /**
     * 实际结束时间
     *
     * @return
     */
    @PutMapping("/endTime/{id}")
    @ApiOperation(value = "实际结束时间", response = ObjRes.class)
    public ObjRes<Void> endTime(@PathVariable Long id) {
        try {
            return iTaskService.endTime(id);
        } catch (TaskHasEndedException e) {
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.TASK_HAS_ENDED.getCode(), ResultCodeEnum.TASK_HAS_ENDED.getLabel());
        }
    }
}